//
//  SWStatusCell.swift
//  MYSwiftWeiBo
//
//  Created by shingwai chan on 16/3/4.
//  Copyright © 2016年 ChanShingWai. All rights reserved.
//

import UIKit
import SnapKit


class SWStatusCell: UITableViewCell {

    var buttonViewTopConstraint: Constraint?
    
    var status: SWStatus? {
        didSet{
            //当控制器给cell设置status模型时,将模型传递给 originalView
            originalView.status = status
            //判断是否需要显示转发的view
            buttonViewTopConstraint?.uninstall()
            if let retweeted_status = status?.retweeted_status {
                retweetView.hidden = false
                //赋值模型
                retweetView.status = retweeted_status
                //重新给底部约束赋值
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                  self.buttonViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            }else{
                retweetView.hidden = true
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.buttonViewTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        setUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI() {
    
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        //原创微博的约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
        }
//        被转发内容
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
        }
        
        //底部按钮view约束
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
           self.buttonViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(bottomView)
        }
        
        
        
    
    }

    //MARK:懒加载
    //微博view
    lazy var originalView:SWStatusOriginalView = SWStatusOriginalView()
    //底部view
    lazy var bottomView: SWStatusBottomView = SWStatusBottomView()
    //被转发内容view
    lazy var retweetView: SWStatusRetweetView  = SWStatusRetweetView()
}
